<?php
// +----------------------------------------------------------------------
// | 规格名控制器
// +----------------------------------------------------------------------
// | 2020-4-10
// +----------------------------------------------------------------------
// | Author: 周`
// +----------------------------------------------------------------------

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use think\App;
use think\facade\Db;
use think\facade\View;

class ItemSpec extends AdminController
{
    /**
     * 实例化一个模型实例.
     *
     * @return void
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\mall\Item');
        $this->ItemSku = app('app\common\model\mall\ItemSku');
        $this->ItemSkuSpec = app('app\common\model\mall\ItemSkuSpec');
        $this->ItemSpec = app('app\common\model\mall\ItemSpec');
        $this->ItemSpecValue = app('app\common\model\mall\ItemSpecValue');
        $this->ItemCategory = app('app\common\model\common\Categories');
    }
    
    /**
     * 添加规格
     * @param ArticleRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store()
    {
        $data = $this->request->param();
        try{
            Db::startTrans();
            
           if($this->ItemSpec->where('spec_name',$data['spec_name'])->find()){
                return json(['code'=>1,'msg'=>'规格名已存在']);
            }
            if($this->ItemSpecValue->where(['spec_value'=>$data['spec_value']])->find()){
                return json(['code'=>1,'msg'=>'规格值已存在']);
            }
            $data['app_id'] = $this->model->getApp();
            $spec = $this->ItemSpec->save($data);
            $data['spec_id'] = $this->ItemSpec->spec_id;
            
            $specValue = $this->ItemSpecValue->save($data);
           Db::commit();
           return json(['code'=>200,'msg'=>'添加成功','data'=>['spec_id'=>$this->ItemSpec->spec_id,'spec_value_id'=>$this->ItemSpecValue->spec_value_id]]);
        }catch (\Exception $exception){
              Db::rollBack();
            return json(['code'=>1,'msg'=>'添加失败','data'=>$exception->getMessage()]);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function destroy()
    {
        $ids = $this->request->param('ids');
        if (!is_array($ids) || empty($ids)){
            return json(['code'=>1,'msg'=>'请选择删除项']);
        }

        $category = $this->ItemSkuSpec->where('spec_id',$ids[0])->find();
        Db::startTrans();
        try{
            if ($category){
              return json(['code'=>1,'msg'=>'该规格下有商品，不能删除']);
            }

            $row = $this->ItemSpecValue->whereIn('spec_id',$ids)->select();
            $row->delete();
            $rows = $this->ItemSpec->whereIn('spec_id',$ids)->select();
            empty($rows) && $this->error('数据不存在');
            $save = $rows->delete();

            Db::commit();
            return json(['code'=>200,'msg'=>'删除成功']);
        }catch (\Exception $exception){
            Db::rollback();
            return json(['code'=>1,'msg'=>'删除失败','data'=>$exception->getMessage()]);
        }
    }

}
